package fp.poczadly.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import fp.poczadly.client.LiveChatService;
import fp.poczadly.shared.Message;
import fp.poczadly.shared.User;
import fp.poczadly.shared.Utils;

@SuppressWarnings( "serial" )
public class LiveChatServiceImpl extends RemoteServiceServlet implements LiveChatService {

	private final Map<String, User> users = new HashMap<String, User>();
	
	private final List<Message> messageList = new ArrayList<Message>();
	
	protected HttpSession getSession() {
		return this.getThreadLocalRequest().getSession( true );
	}
	
	protected<T> void setValue( String param, T t ) {
		getSession().setAttribute( param, t );
	}
	
	protected<T> T getValue( String param ) {
		return (T) getSession().getAttribute( param );
	}
	
	@Override
	public User checkSession( String param ) throws NullPointerException {
		if( getValue( Utils.USER ) != null )
			return (User) getValue( Utils.USER );
		else
			throw new NullPointerException();
	}
	
	@Override
	public User addUser( User user ) throws Exception {
		// walidacja
		if( users.containsKey( user.getUsername() ) ) {
			throw new Exception();
		} else {
			users.put( user.getUsername(), user );
		}
		
		setValue( Utils.USER, user );
		
		return user;
	}

	@Override
	public User removeUser( User user ) {
		users.remove( user.getUsername() );
		
		setValue( Utils.USER, null );
		
		return user;
	}
	
	@Override
	public User getSelectedUser( String username ) throws NullPointerException {
		if( users.containsKey( username ) ) {
			return users.get( username );
		} else {
			throw new NullPointerException();
		}
	}
	
	@Override
	public List<Message> receiveMessages() {
		List<Message> shoutBoxList = new ArrayList<Message>();
		
		for( Message message : messageList ) {
			if( message.getTo() == null )
				shoutBoxList.add( message );
		}
		
		return shoutBoxList;
	}
	
	@Override
	public List<Message> receivePrivateMessages( User user ) {
		List<Message> privateMesageList = new ArrayList<Message>();
		
		for( Message message : messageList ) {
			if( ( message.getTo() != null && message.getTo().getUsername().equals( user.getUsername() ) ) || ( message.getTo() != null && message.getFrom().getUsername().equals( user.getUsername() )  ) )
				privateMesageList.add( message );
		}
		
		return privateMesageList;
	}
	
	@Override
	public void removePrivateMessages( User from ) {
		for( Message message : receivePrivateMessages( from ) ) {
			messageList.remove( message );
		}
	}

	@Override
	public void sendMessage( Message message ) {
		messageList.add( message );
	}

	@Override
	public List<String> getAllUsers() {
		List<String> userLoginList = new ArrayList<String>();
		
		for( String user : users.keySet() ) {
			userLoginList.add( user );
		}
		
		return userLoginList;
	}

}
